package org.jeecgframework.core.util.clean;

import jeecg.business.entity.site.SiteEntity;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;

public class BaiduVideoHtmlCleanUtil{

	public static HtmlParseResult fetchBaiduVideo4Html(String href , String charset , SiteEntity site , String cookies){
		HtmlParseResult result = null;
		try{
			result = new HtmlParseResult();
			
			String html = HttpClientUtil.getSourceFromUrl(href, charset, null);
//			System.out.println(html);
			html = html.replace("{xml version='1.0' encoding='UTF-8' ?>", "");
			html = html.replace("/**/videoJson(", "");
	        html = html.substring(0,html.length()-1);
	        /**
	         * 
	         */
	        html = html.substring(0,html.indexOf("filters:"))+html.substring(html.indexOf("data:"));
//	        html = StringUtil.removeHTMLLable(html);
	        JSONObject jobj = JSONObject.fromObject(html);
			JSONArray dataResult = jobj.getJSONArray("data");
			JSONObject data = null;
			if(dataResult != null){
				String url = null;
				String title = null;
				HtmlParseResult each = null;
				for(int i=0;i<dataResult.size();i++){
					data = dataResult.getJSONObject(i);
					url = data.getString("url");
					title = data.getString("ti");
					each = new HtmlParseResult();
		            each.setType(site.getJclx());
		            each.setUrl(url);
		            each.setTitle(title);
		            result.addChildResult(each);
				}
			}
//			System.out.println(nodes.getLength());
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return result;
	}

}
